package btb.engine;

import java.io.IOException;

import com.esotericsoftware.minlog.Log;

import btb.net.CChatServer;
import btb.net.CMetaServer;
import btb.util.CLogger;

public class CMainServer extends ATask {
	public CTaskMessenger mailbox;
	
	private CMetaServer metaServer;
	private CChatServer chatServer;
	
	public CMainServer() {
		super(CTaskMessenger.TASK_MAIN);
		this.mailbox = new CTaskMessenger();
	}
	
	public void update() {

	}
	
	public void handleEvent(CEvent _e) {}
	
	public void run() {
		while(!this.die) {
			this.update();
		}
	}
	
	public static void main(String args[]) {
		CLogger logger = new CLogger();
		logger.toFile(false);
		
		Log.set(Log.LEVEL_DEBUG);
		Log.setLogger(logger);
		
		CMainServer mainServer = new CMainServer();
		
		mainServer.metaServer = new CMetaServer(mainServer, CTaskMessenger.TASK_MSERVER, mainServer.mailbox);
			mainServer.metaServer.setFriendlyName("Kami");
			
		try
		{
			mainServer.chatServer = new CChatServer(mainServer, CTaskMessenger.TASK_CSERVER, mainServer.mailbox);
		}
		catch (IOException e)
		{
			CLogger.cond_error("chat server", "Chat server failed to instantiate.");
			e.printStackTrace();
		}
			mainServer.metaServer.setFriendlyName("Hove");
		
		if(mainServer.metaServer.setUp())
			mainServer.metaServer.start();
		else {
			if(Log.WARN)
				Log.warn("engine", "Unable to create metaServer");
		}
		
		mainServer.run();
	}

	@Override
	public void handleDeath() {
		// TODO Auto-generated method stub
		
	}
}
